﻿/////////////////////////////////////////////////////////////////////////////////
//
// PlainText
// - Gets rid of unwanted formatting
// (C) Copyright Johan Åhlén 2009
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
// See the GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see http://www.gnu.org/licenses/.
//
// More info on PlainText website: http://plaintext.codeplex.com
//
/////////////////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace AdvantIQ.PlainText
{
    internal class Filter
    {
        internal string ID;
        internal string Description;
        internal string RegexOptions;
        internal string MatchPattern;
        internal Regex FindExpression;
        internal string ReplaceString;

        public Filter(string id, string description, string regexOptions, string matchPattern, string replaceString)
        {
            ID = id;
            Description = description;
            RegexOptions = regexOptions;
            MatchPattern = matchPattern;
            FindExpression = new Regex(matchPattern, (RegexOptions)Enum.Parse(typeof(RegexOptions), regexOptions));
            ReplaceString = replaceString;

            if (FindExpression == null)
                throw new Exception("Invalid match pattern");
        }

        public string Apply(string clipboardText)
        {
            var replaceString = ReplaceString.Replace("\\r", "\r").Replace("\\n", "\n");
            return FindExpression.Replace(clipboardText, replaceString);
        }

        public static IEnumerable<string> GetAllFilterIDs()
        {
            var lst = new List<string>();

            foreach (var filterData in Properties.Settings.Default.Filters)
            {
                var split = filterData.Split('|');
                lst.Add(split[0]);
            }

            return lst;
        }

        public static IEnumerable<string> GetEnabledFilterIDs()
        {
            return Properties.Settings.Default.EnabledFilters.Split('|');
        }

        public static void SetEnabledFilters(string[] filterIDs)
        {
            Properties.Settings.Default.EnabledFilters = string.Join("|", filterIDs);
        }

        public static IEnumerable<Filter> GetAllFilters()
        {
            var lst = new List<Filter>();

            foreach (var filterData in Properties.Settings.Default.Filters)
            {
                var split = filterData.Split('|');
                lst.Add(new Filter(split[0], split[1], split[2], split[3], split[4]));
            }

            return lst;
        }

        public static IEnumerable<Filter> GetEnabledFilters()
        {
            var ids = GetEnabledFilterIDs();

            return from filter in GetAllFilters()
                   where ids.Contains(filter.ID)
                   select filter;
        }

        public static Filter GetFilterByID(string filterID)
        {
            return GetAllFilters().Single(f => f.ID == filterID);
        }

        public static void DeleteFilter(string filterID)
        {
            var filterStrings = new StringCollection();
            foreach (var filter in GetAllFilters().Where(f => f.ID != filterID))
                filterStrings.Add(filter.ToString());

            Properties.Settings.Default.Filters = filterStrings;
            Properties.Settings.Default.Save();
        }

        public static void UpdateOrAddFilter(Filter filter)
        {
            var filterStrings = new StringCollection();
            foreach (var f in GetAllFilters().Where(f => f.ID != filter.ID))
                filterStrings.Add(f.ToString());
            filterStrings.Add(filter.ToString());

            Properties.Settings.Default.Filters = filterStrings;
            Properties.Settings.Default.Save();
        }

        public override string ToString()
        {
            return string.Format("{0}|{1}|{2}|{3}|{4}", ID, Description, RegexOptions, MatchPattern, ReplaceString);
        }
    }
}
